﻿Imports DevExpress.XtraGrid.Views.Grid
Public Class frmDepartments
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Dim Primary_Code As String
#Region "Function"
    Sub Load_Data()
        Try
            Strsql = "Select * from department"
            With GridDepartment
                Dim DT_department As New DataTable
                DT_department = _mysql.GetMYSQLDataTable(Strsql, "department")
                .DataSource = DT_department
                With GridColumn1
                    .FieldName = "Department_ID"

                End With

                With GridColumn2
                    .FieldName = "Department_Name"

                End With
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
#End Region



    Private Sub frmDepartments_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Data()
    End Sub

    Private Sub MNAddDep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MNAddDep.Click
        If frmNewDepartment.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Load_Data()
    End Sub

    Private Sub MnEditDep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnEditDep.Click
        frmEditDepartment.Primary_Code = Primary_Code
        If frmEditDepartment.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
        Load_Data()
    End Sub

    Private Sub GridView1_CustomRowCellEditForEditing(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) Handles GridView1.CustomRowCellEditForEditing
        Try

            Dim Gv As GridView = sender
            Dim CodeName As String

            'Dim TypeCODE As String = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Item_Type")).ToString()
            If Me.SearchOption.EditValue = 0 Then
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Department_ID")).ToString()
            Else
                CodeName = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Department_Name")).ToString()
            End If
            Primary_Code = Gv.GetRowCellValue(e.RowHandle, Gv.Columns("Department_ID")).ToString()

            Me.TxtSearch.EditValue = CodeName
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub MnDeleteDep_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnDeleteDep.Click
        Try
            If Primary_Code = -1 Then Exit Sub
            If (MsgBox("กรุณายืนยันการลบข้อมูล " & Primary_Code, MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            If (MsgBox("การลบข้อมูลเมื่อยืนยันแล้วจะไม่สามารถนำกลับมาใหม่ได้ คุณยังต้องการยืนยันใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "ยืนยันการลบข้อมูล") = MsgBoxResult.No) Then Exit Sub
            Strsql = "Select count(*) as cc from department where Department_ID = " & Primary_Code
            If (_mysql.MySQLExecuteScalar(Strsql)) = 0 Then
                MsgBox("ไม่พบรหัสที่ต้องการลบ", MsgBoxStyle.Critical, "กรุณาตรวจสอบรายการลบ")
                Exit Sub
            End If


            Strsql = "Delete from department where  Department_ID = '" & Primary_Code & "'"

            Select Case (_mysql.MySQLExecuteScalar(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteDep_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                        Exit Sub
                    End With

                Case Else
                    MsgBox("ลบข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการลบข้อมูล")


            End Select

            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (16,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & Primary_Code & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1
                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "MnDeleteDep_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()

                    End With
            End Select

            Load_Data()
        Catch ex As Exception
            ' MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub GridDepartment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridDepartment.Click

    End Sub
End Class